package zero.tech.redis.topic.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import zero.tech.redis.topic.TopicCMD;
import zero.tech.redis.topic.TopicChannelType;
import zero.tech.redis.topic.service.api.IRedisMsgService;

@Service
public class RedisMsgServiceImpl implements IRedisMsgService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

	@Override
	public void pulishRedisTopic(TopicChannelType channelType, String orderType, Object obj) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("redis_order", orderType);
		String paramString=null;
		if(obj instanceof JSONObject||obj instanceof JSON) {
			paramString=((JSONObject) obj).toJSONString();
		}else {
			paramString=JSONObject.toJSONString(obj);
		}
		if (StringUtils.isNotBlank(paramString)) {
			jsonObject.put("param_string", paramString);
		}
		if (!orderType.equals(TopicCMD.TEST)) {
			redisTemplate.convertAndSend(channelType == null ? "default" : channelType.getChannelType(),jsonObject);
		} else {
			redisTemplate.convertAndSend(channelType == null ? "default" : channelType.getChannelType(),jsonObject);
		}
	}
}
